route.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { NextResponse } from "next/server";
  2. import smbClient from "@/lib/smbClient";
  3. export async function GET(_, { params }) {
  4. const { path: pathArray = [] } = params;
  5. let smbPath = pathArray.join("\\");
  6. console.log("Versuche, folgende Datei zu öffnen:", smbPath);
  7. return new Promise((resolve, reject) => {
  8. smbClient.readFile(smbPath, async (err, file) => {
  9. if (err) {
  10. console.error("Fehler beim Zugriff auf die Datei:", err);
  11. let errorMessage = "Fehler beim Zugriff auf die Datei";
  12. if (err.code === "ENOENT") {
  13. errorMessage = "Datei nicht gefunden";
  14. } else if (err.code === "EACCES") {
  15. errorMessage = "Zugriff verweigert";
  16. }
  17. reject(
  18. NextResponse.json(
  19. {
  20. error: errorMessage,
  21. details: err.message,
  22. },
  23. { status: 500, headers: { "Cache-Control": "no-store" } }
  24. )
  25. );
  26. } else {
  27. console.log("Erfolgreicher Zugriff auf die Datei:", smbPath);
  28. // Rückgabe der Datei mit Cache-Control und Content-Disposition Header
  29. resolve(
  30. new NextResponse(file, {
  31. status: 200,
  32. headers: {
  33. "Content-Type": "application/pdf",
  34. "Cache-Control": "no-store",
  35. "Content-Disposition": "inline", // PDF im Browser anzeigen
  36. },
  37. })
  38. );
  39. }
  40. });
  41. });
  42. }